<?php

/**
 * @file IndexController
 * @author garypradoruiz <garypradoruiz@gmail.com>
 * @link mailto:garypradoruiz@gmail.com
 * @copyleft 2012 garypradoruiz
 * @license GPL
 * @version 1
 */
class Solicitud_AdjuntosController extends Zend_Controller_Action {

    public $type_upload = array('gif', 'jpg', 'jpeg', 'png', 'tif', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pps', 'ppsx', 'psd', 'txt', 'rtf','zip');

    public function init() {
        /* Initialize action controller here */
    }

    public function uploadAction() {
        
    }

    
    public function downloadAttachmentAction() {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender();

        $request = $this->getRequest();
        $id_attach = $request->getParam('id', 0);

        $attachmentDB = new Solicitud_Model_DbTable_SolicitudAttachment();
        $where = $attachmentDB->getAdapter()->quoteInto('id=?', $id_attach);
        $dataAttachment = $attachmentDB->fetchRow($where);

        $url_file = realpath(APPLICATION_PATH . '/../public/uploads/') . '/' . $dataAttachment->name_saved;

        header('Content-Type: ' . $dataAttachment->content_type);
        header('Content-Disposition: attachment; filename=' . basename($dataAttachment->name_original));
        header("Content-Transfer-Encoding: binary");
        readfile($url_file);
    }

    public function subirAdjuntoAction() {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender();

        $request = $this->getRequest();
        $attachmentDB = new Solicitud_Model_DbTable_SolicitudAttachment();

        if ($request->isXmlHttpRequest()) {

            $upload = new Zend_File_Transfer_Adapter_Http();

            $filename = $upload->getFileName();
            $files = $upload->getFileInfo();

            $upload->addValidator('Size', false, '5MB');
            $upload->addValidator('Extension', false, $this->type_upload);

            $path = realpath(APPLICATION_PATH . '/../public/uploads');

            $filename_saved = time() . $files['myfile']['name'];
            $filename_saved = str_replace(" ", "_", $filename_saved);
            $upload->addFilter('Rename', $path . '/' . $filename_saved, $files[0]);

            $extension = pathinfo($filename, PATHINFO_EXTENSION);

            if (!$upload->isValid()) {
                $msg = $upload->getMessages();
                if (isset($msg['fileExtensionFalse'])) {
                    echo 'Ups !! Tipo de archivo no soportado... :(';
                } else {
                    if (isset($msg['fileSizeTooBig']))
                        echo 'El archivo pesa demasiado, maximo 5 Mb :)';
                }
                die();
            } else {
                try {
                    if ($upload->receive($files[0])) {
                        $data = array(
                            'name_original' => str_replace(" ", "_", $files['myfile']['name']),
                            'name_saved' => $filename_saved,
                            'description' => $request->getParam('description_file'),
                            'extension' => $extension,
                            'content_type' => $files['myfile']['type'],
                            'status' => 1,
                            'code'  => $request->getParam('code')
                        );
                        $lastInsert = $attachmentDB->insert($data);

                        //get all data attachment of ID Order
                        $where = $attachmentDB->getAdapter()->quoteInto('id=?', $lastInsert);
                        $dataAttachment = $attachmentDB->fetchRow($where);
                        $jsonData = Zend_Json::encode($dataAttachment->toArray());
                        echo $jsonData;
                    }
                } catch (Zend_File_Transfer_Exception $e) {
                    $e->getMessage();
                }
            }
        }
    }

    public function getAttachmentTempAction() {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        $request = $this->_request;
        $code = $request->getParam('code', '');
        $attachmentDB = new Attachment_Model_DbTable_AttachmentTemp();
        $result = $attachmentDB->fetchAll("code = '". $code ."'");
        if ($result != null) {
            $jsonData = Zend_Json::encode($result->toArray());
            echo $jsonData;
            exit;
        }
    }
    
    public function borrarAdjuntoAction() {
        $request = $this->getRequest();
        $id_attach = $request->getParam('id', 0);

        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender();

        $attachmentDB = new Solicitud_Model_DbTable_SolicitudAttachment();

        if ($request->isXmlHttpRequest()) {
            $where = $attachmentDB->getAdapter()->quoteInto('id=?', $id_attach);
            $attachmentDB->delete($where);
            echo 'attachment - solicitud - deleted.';
        }
    }

}

?>